﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Linq;
using System.ServiceModel.DomainServices.EntityFramework;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;
using Quidsi.Services.Web;
using Quidsi.Services.Web.Models;

namespace Quidsi.Model.Services
{

    // Implements application logic using the QuidsiEntities context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    [RequiresAuthentication]
    [EnableClientAccess()]
    public class QuidsiService : QuidsiBaseService
    {
        public QuidsiService(User currentUser) : base(currentUser) { }

        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Features' query.
        public IQueryable<Feature> GetFeatures()
        {
            return this.ObjectContext.Features;
        }

        public void InsertFeature(Feature feature)
        {
            if ((feature.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(feature, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Features.AddObject(feature);
            }
        }

        public void UpdateFeature(Feature currentFeature)
        {
            this.ObjectContext.Features.AttachAsModified(currentFeature, this.ChangeSet.GetOriginal(currentFeature));
        }

        public void DeleteFeature(Feature feature)
        {
            if ((feature.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Features.Attach(feature);
            }
            this.ObjectContext.Features.DeleteObject(feature);
        }

        // Consider
        // 1. Adding parameters to this method and constraining returned results, and/or
        // 2. Adding query methods taking different parameters.
        public IQueryable<TeamLead> GetTeamLeads()
        {
            return this.ObjectContext.TeamLeads.Where(tl => tl.IsActive);
        }

        public IQueryable<TeamLead> GetTeamLeadById(int id)
        {
            return this.ObjectContext.TeamLeads.Where(tl => tl.Id == id && tl.IsActive);
        }

        public void InsertTeamLead(TeamLead teamLead)
        {
            SetAuditingFields(teamLead, true);
            if ((teamLead.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(teamLead, EntityState.Added);
            }
            else
            {
                this.ObjectContext.TeamLeads.AddObject(teamLead);
            }
        }

        public void UpdateTeamLead(TeamLead currentTeamLead)
        {
            SetAuditingFields(currentTeamLead, false);
            this.ObjectContext.TeamLeads.AttachAsModified(currentTeamLead, this.ChangeSet.GetOriginal(currentTeamLead));
        }

        public void DeleteTeamLead(TeamLead teamLead)
        {
            if ((teamLead.EntityState == EntityState.Detached))
            {
                this.ObjectContext.TeamLeads.Attach(teamLead);
            }
            this.ObjectContext.TeamLeads.DeleteObject(teamLead);
        }

        // Consider
        // 1. Adding parameters to this method and constraining returned results, and/or
        // 2. Adding query methods taking different parameters.
        public IQueryable<TeamLeadResource> GetTeamLeadResources()
        {
            return this.ObjectContext.TeamLeadResources;
        }

        public IQueryable<TeamLeadResource> GetTeamLeadResourcesById(int id)
        {
            return this.ObjectContext.TeamLeadResources.Where(r => r.TeamLeadId == id).OrderBy(r => r.EffectiveDate);
        }


        public void InsertTeamLeadResource(TeamLeadResource teamLeadResource)
        {
            SetAuditingFields(teamLeadResource, true);
            if ((teamLeadResource.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(teamLeadResource, EntityState.Added);
            }
            else
            {
                this.ObjectContext.TeamLeadResources.AddObject(teamLeadResource);
            }
        }

        public void UpdateTeamLeadResource(TeamLeadResource currentTeamLeadResource)
        {
            SetAuditingFields(currentTeamLeadResource, false);
            this.ObjectContext.TeamLeadResources.AttachAsModified(currentTeamLeadResource, this.ChangeSet.GetOriginal(currentTeamLeadResource));
        }

        public void DeleteTeamLeadResource(TeamLeadResource teamLeadResource)
        {
            if ((teamLeadResource.EntityState == EntityState.Detached))
            {
                this.ObjectContext.TeamLeadResources.Attach(teamLeadResource);
            }
            this.ObjectContext.TeamLeadResources.DeleteObject(teamLeadResource);
        }
    }
}


